package com.shuqi.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.Log4an;
import com.shuqi.model.manager.MyAccountManager;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.AccountBalancePaser;

/**
 * 订单页面数据
 * 
 * @author yanghuawei
 * 
 */
public class RefreshBalanceModel extends ModelBase implements HttpRequestListener {
    private final String logTag = "Order";
    private HttpRequest httpRequest;
    private Context mContext;
    private boolean isLoading = false;

    public RefreshBalanceModel(Context context) {
        this.mContext = context;
    }

    public boolean isLoading() {
        return isLoading;
    }

    @Override
    public void action(int actionCode, Object object) {
        isLoading = false;
        Log4an.e(logTag, "action:" + actionCode);
        fireEvent(actionCode, object);
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        long timestamp = System.currentTimeMillis();
        String key = "4d30ef50cbd32ed92deb6fbd2321e951";// 充值记录
        String strTimestamp = timestamp + "";
        String md5_key_sb = key + timestamp;
        String md5_key = com.shuqi.common.utils.Util.MD5(md5_key_sb.toString());
        params.add(new BasicNameValuePair("key", md5_key));// md5
        params.add(new BasicNameValuePair("timestamp", strTimestamp));// 时间戳
        params.add(new BasicNameValuePair("userid", MyAccountManager.getInstance(mContext)
                .getUserId()));
        return params;
    }

    @Override
    public String getRequestUrl() {
        return Urls.getPDHistory(false,"");
    }

    @Override
    public void onDestroy() {
        if (httpRequest != null) {
            httpRequest.abort();
        }
    }

    @Override
    public void loadData(Object... objs) {
        httpRequest =
                new HttpRequest(mContext, UrlManager.COLUMN_DOMAIN_PAY, getRequestUrl(), setParams(),
                        this);
        httpRequest.setDataParseAdapter(new AccountBalancePaser());
        isLoading = true;
        MyTask.runInBackground(httpRequest, true);
    }
}
